package com.shiwei.pdf.demo;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.lowagie.text.List;

import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

@RestController
public class JasperController {
	
	@GetMapping("/testJasper")
	public void createPdf(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Resource resource = new ClassPathResource("walls.jasper");
		ServletOutputStream os = response.getOutputStream();
		
		try {
			Map<String, Object> parameters = new HashMap<String, Object>();
			parameters.put("pageNum","202106170001");
			parameters.put("inputName","admin");
			parameters.put("inputAddress","上海交银大厦");
			parameters.put("inputTime","2021/6/18 am");
			parameters.put("inputImgPath","/home/insky/图片");
			parameters.put("inputImgNum","5000");
			//parameters.put("imgUrl","https://pics7.baidu.com/feed/86d6277f9e2f07087d8bbc4d9c231391a801f285.jpeg?token=d25e8e54fb23fdabe1f9c214350a7ad1");

			parameters.put("modelFile","xxxxx.weights");
			parameters.put("modelVersion","v1.0.12");
			parameters.put("modelTime","2021/6/18 09:12:56");
			parameters.put("modelPersent","0.93");
			parameters.put("modelRecycle","0.90");
			
			
			parameters.put("confidentThreshold1","置信度阈值 0.2");
			parameters.put("confidentThreshold2","置信度阈值 0.3");
			parameters.put("confidentThreshold3","置信度阈值 0.4");
			parameters.put("confidentThreshold4","置信度阈值 0.4");
			parameters.put("confidentThreshold5","置信度阈值 0.2");
			parameters.put("confidentThreshold6","置信度阈值 0.4");
			parameters.put("confidentThreshold7","置信度阈值 0.4");
			
			parameters.put("IOUThreshold1","IOU阈值 0.45");
			parameters.put("IOUThreshold2","IOU阈值 0.45");
			parameters.put("IOUThreshold3","IOU阈值 0.45");
			parameters.put("IOUThreshold4","IOU阈值 0.45");
			parameters.put("IOUThreshold5","IOU阈值 0.45");
			parameters.put("IOUThreshold6","IOU阈值 0.45");
			parameters.put("IOUThreshold7","IOU阈值 0.45");
			
			parameters.put("detectTimeStart","2021/6/18 09:12:56");
			parameters.put("detectTimeEnd","2021/6/18 09:45:00");
			parameters.put("detectCost","0h32min04sec");
			
			parameters.put("picNum1","200");
			parameters.put("picNum2","5");
			parameters.put("picNum3","15");
			parameters.put("picNum4","20");
			parameters.put("picNum5","150");
			parameters.put("picNum6","50");
			parameters.put("picNum7","32");
			parameters.put("picNum8","12");
			parameters.put("picNum9","50");
			parameters.put("picNum10","21");
			parameters.put("picNum11","9");
			parameters.put("picNum12","18");
			parameters.put("picNum13","60");
			parameters.put("picSum","642");
			
			
			parameters.put("aimNum1","218");
			parameters.put("aimNum2","5");
			parameters.put("aimNum3","16");
			parameters.put("aimNum4","20");
			parameters.put("aimNum5","162");
			parameters.put("aimNum6","56");
			parameters.put("aimNum7","36");
			parameters.put("aimNum8","15");
			parameters.put("aimNum9","50");
			parameters.put("aimNum10","21");
			parameters.put("aimNum11","10");
			parameters.put("aimNum12","22");
			parameters.put("aimNum13","67");
			parameters.put("aimSum","688");
			
			parameters.put("dpmNum1","4.00%");
			parameters.put("dpmNum2","0.10%");
			parameters.put("dpmNum3","0.30%");
			parameters.put("dpmNum4","0.40%");
			parameters.put("dpmNum5","3.00%");
			parameters.put("dpmNum6","1.00%");
			parameters.put("dpmNum7","0.64%");
			parameters.put("dpmNum8","0.24%");
			parameters.put("dpmNum9","1.00%");
			parameters.put("dpmNum10","0.42%");
			parameters.put("dpmNum11","0.18%");
			parameters.put("dpmNum12","0.36%");
			parameters.put("dpmNum13","0.12%");
			parameters.put("dpmSum","12.84%");
			
			//JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(getFaultsList());
			//Resource subResource = new ClassPathResource("wall_sub.jasper"); 
			//parameters.put("subPath",subResource.getFile().getPath());
			parameters.put("subList",getFaultsList());
			
			JasperPrint print = JasperFillManager.fillReport(resource.getFile().getAbsolutePath(), parameters, new JREmptyDataSource());
			JasperExportManager.exportReportToPdfStream(print, os);
			response.setContentType("application/pdf");
			response.setHeader("Content-Disposition", "attachment;fileName=jasper.pdf");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			os.flush();
			os.close();
		}
	}
	
	
	public ArrayList<FaultsBean> getFaultsList() throws Exception { 
		ArrayList<FaultsBean> list = new ArrayList<FaultsBean>();
		String imgPath = "https://pics7.baidu.com/feed/86d6277f9e2f07087d8bbc4d9c231391a801f285.jpeg?token=d25e8e54fb23fdabe1f9c214350a7ad1";
		for (int i=1;i<=5;i++) {
			FaultsBean fault = new FaultsBean(i+"", "玻璃面板破损", imgPath, "文件名：xxxxx.jpg\r\n"
					+ "缺陷类别：玻璃面板破损\r\n"
					+ "缺陷位置：\r\n"
					+ "0 0.4255 0.4257 0.0896 0.5788\r\n"
					+ "GPS 位置：\r\n"
					+ "31.1134332,121.6742189,18.26\r\n"
					+ "拍摄日期：2021/6/16 9：23"); 
			list.add(fault);
		}
		for (int i=1;i<=5;i++) {
			FaultsBean fault = new FaultsBean(i+"", "石材面板裂纹", imgPath, "文件名：xxxxx.jpg\r\n"
					+ "缺陷类别：石材面板裂纹\r\n"
					+ "缺陷位置：\r\n"
					+ "0 0.4255 0.4257 0.0896 0.5788\r\n"
					+ "GPS 位置：\r\n"
					+ "31.1134332,121.6742189,18.26\r\n"
					+ "拍摄日期：2021/6/16 9：23"); 
			list.add(fault);
		}
		return list;
	}

	//创建数据库Connection
	public Connection getConnection() throws Exception { 
		String url = "jdbc:mysql://124.70.140.183:3306/third_eye?charset=utf8mb4";
		Class.forName("com.mysql.cj.jdbc.Driver");
		Connection conn = DriverManager.getConnection(url, "root", "Wen_315845");
		return conn;
	}

}
